//
//  CSWSettingViewController.m
//  彩票
//
//  Created by xiaomage on 15/9/24.
//  Copyright © 2015年 xiaomage. All rights reserved.
//

#import "CSWSettingViewController.h"

#import "CSWSettingItem.h"
#import "CSWSettingArrowItem.h"
#import "CSWSettingSwitchItem.h"

#import "CSWGroupItem.h"

#import "CSWSettingCell.h"

#import "CSWPushViewController.h"

@interface CSWSettingViewController ()

// 保存当前tableView有多少组,元素应该是一个groupItem
@property (nonatomic, strong) NSMutableArray *groups;

@end

@implementation CSWSettingViewController

- (NSMutableArray *)groups
{
    if (_groups == nil) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}

- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 手动创建模型数据
    [self setUpNavButton];
    // 第一组
    [self setUpGroup0];
    
    [self setUpGroup1];
    
    [self setUpGroup2];
}

- (void)setUpNavButton
{
    self.title = @"设置";
    
    UIBarButtonItem *help = [[UIBarButtonItem alloc] initWithTitle:@"常见问题" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.rightBarButtonItem = help;
}

// 第0组
- (void)setUpGroup0
{
    CSWGroupItem *group = [[CSWGroupItem alloc] init];
    group.headTitle = @"abc";
    CSWSettingArrowItem *item = [CSWSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"];
    
    group.items = @[item];
    
    [self.groups addObject:group];
}

// 第1组
- (void)setUpGroup1
{
    // 创建组模型
    CSWGroupItem *group = [[CSWGroupItem alloc] init];
    group.headTitle = @"bcd";
    
    // 描述这一组有多少行
    CSWSettingArrowItem *item = [CSWSettingArrowItem itemWithImage:[UIImage imageNamed:@"MorePush"] title:@"推送和提醒"];
    // 描述需要跳转的控制器
    item.descVc = [CSWPushViewController class];
    
    CSWSettingSwitchItem *item1 = [CSWSettingSwitchItem itemWithImage:[UIImage imageNamed:@"more_homeshake"] title:@"使用摇一摇随机选"];
    
    CSWSettingSwitchItem *item2 = [CSWSettingSwitchItem itemWithImage:[UIImage imageNamed:@"sound_Effect"] title:@"声音效果"];
    CSWSettingSwitchItem *item3 = [CSWSettingSwitchItem itemWithImage:[UIImage imageNamed:@"More_LotteryRecommend"] title:@"购彩小助手"];
    
    group.items = @[item,item1,item2,item3];
    
    [self.groups addObject:group];
    
}


// 第2组
- (void)setUpGroup2
{
    
    // 创建组模型
    CSWGroupItem *group = [[CSWGroupItem alloc] init];
    
    // 描述这一组有多少行
    CSWSettingArrowItem *item = [CSWSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"检查新版本"];
    item.operationBlock = ^(){
        NSLog(@"检查新版号");
    };
    
    CSWSettingArrowItem *item1 = [CSWSettingArrowItem itemWithImage:[UIImage imageNamed:@"MoreShare"] title:@"分享"];
    item1.operationBlock = ^(){
        
        NSLog(@"没有最新版本");
    };
    CSWSettingArrowItem *item2 = [CSWSettingArrowItem itemWithImage:[UIImage imageNamed:@"MoreNetease"] title:@"产品推荐"];
    CSWSettingArrowItem *item3 = [CSWSettingArrowItem itemWithImage:[UIImage imageNamed:@"MoreAbout"] title:@"关于"];
    
    item3.descVc = [CSWPushViewController class];
    
    group.items = @[item,item1,item2,item3];
    
    [self.groups addObject:group];
}


#pragma mark - tableView数据源
// 返回有多少组
- (NSInteger)numberOfSectionsInTableView:(nonnull UITableView *)tableView{
    return self.groups.count;
}

// 返回每一组有多少行,取出对应的小数组
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CSWGroupItem *group = self.groups[section];
    
    return group.items.count;
}

// 返回每一个cell长什么样子
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    // 创建cell
    CSWSettingCell *cell = [CSWSettingCell cellWithTableView:tableView];
    
    // 获取对应的组模型
    CSWGroupItem *group = self.groups[indexPath.section];
    
    // 获取模型
    CSWSettingItem *item = group.items[indexPath.row];
    
    // 给cell传递一个模型
    cell.item = item;
    
    
    return cell;
}

#pragma mark - tableView代理
// 返回每一组的头部标题
- (NSString *)tableView:(nonnull UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // 获取当前组模型
    CSWGroupItem *group = self.groups[section];
    return group.headTitle;
    
}
//  返回每一组的尾部标题
- (NSString *)tableView:(nonnull UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    
    // 获取当前组模型
    CSWGroupItem *group = self.groups[section];
    return group.footTitle;
    
}

// 只要点击cell就会调用
- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    
    
    // 获取对应的组模型
    CSWGroupItem *group = self.groups[indexPath.section];
    
    // 获取模型
    CSWSettingItem *item = group.items[indexPath.row];
    
    if (item.operationBlock) {
        // 调用block
        item.operationBlock();
        return;
    }
    
    if ([item isKindOfClass:[CSWSettingArrowItem class]]) {
        // 标示箭头类型,可以跳转
        CSWSettingArrowItem *arrowItem = (CSWSettingArrowItem *)item;
        // 获取需要跳转控制器的类名
        
        if (arrowItem.descVc) {
            
            // 创建需要跳转的控制器对象
            UIViewController *vc =   [[arrowItem.descVc alloc] init];
            
            // 跳转界面
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
}
@end